package com.kzd.gtboon.jetpack.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.kzd.gtboon.base.BaseViewModel
import com.kzd.gtboon.entity.*
import com.kzd.gtboon.jetpack.api.*
import com.module.lemlin.http.*
import com.module.lemlin.owner.setRxLifeScope

class HomeViewModel(application: Application) : BaseViewModel(application) {

    private var page: Int = 1
    private var limit: Int = 20

    val changeBarStyleLiveData = MutableLiveData<Int>()
    fun homeChangeBarStyle(distance: Int) {
        changeBarStyleLiveData.postValue(distance)
    }

    val indexHomeLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<HomeIndex>>>()
    fun homeIndex() {
        setRxLifeScope(SubscribeViewModel(indexHomeLiveData)) {
            newIndex()
        }
    }

    val reserveChangeLiveData = MutableLiveData<HttpResponseBody<ReserveChangeResponse>>()
    fun reserveChange(gid: String, type: String, position: Int) {
        setRxLifeScope(object : SubscribeViewModel<ReserveChangeResponse>(reserveChangeLiveData) {
            override fun onSuccess(resp: ReserveChangeResponse) {
                super.onSuccess(resp.apply { this.position = position })
            }
        }) {
            newStarterReserveChange(gid, type)
        }
    }


    val homeDoingsLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataT<Doings<GameDoing>>>>()
    var mGameLayout = ""
    fun homeDoings(
        gameLayout: String, start: Long = 0, page1: Int = 0, isLoadMore: Boolean = false,
    ) {
        this.mGameLayout = gameLayout
        setRxLifeScope(object :
            SubscribeViewModel<HttpResponseDataT<Doings<GameDoing>>>(homeDoingsLiveData) {
            override fun onSuccess(resp: HttpResponseDataT<Doings<GameDoing>>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            if (page1 > 0) {
                page = if (page >= 2) page else page1
            }
            newGameLayout1(gameLayout, start.toString(), page.toString(), limit.toString())
        }
    }

    val homeCouponAreaLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameCoupon>>>()
    fun homeCouponArea(keyword: String? = null, isLoadMore: Boolean = false) {
        setRxLifeScope(object :
            SubscribeViewModel<HttpResponseDataE<GameCoupon>>(homeCouponAreaLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GameCoupon>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            newVoucherGame(keyword, "0", page.toString(), limit.toString())
        }
    }

    val homeCouponEnjoyLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameCoupon>>>()
    fun homeCouponEnjoy() {
        setRxLifeScope(SubscribeViewModel(homeCouponEnjoyLiveData)) {
            newVoucherGame("", "1", page.toString(), "5")
        }
    }


    val homePresentLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GamePresent>>>()
    fun homePresent(keyword: String = "", isLoadMore: Boolean = false) {
        setRxLifeScope(object :
            SubscribeViewModel<HttpResponseDataE<GamePresent>>(homePresentLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GamePresent>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            newGift(keyword, "0", page.toString(), limit.toString())
        }
    }


    val homeServiceTodayLiveData = MutableLiveData<HttpResponseBody<ServiceTitleResponse>>()
    fun homeServiceToday() {
        setRxLifeScope(SubscribeViewModel(homeServiceTodayLiveData)) {
            newGetTodayServer()
        }
    }


    val homeServiceQuickLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataM<String, MutableList<ServiceTitle>>>>()
    fun homeServiceQuick() {
        setRxLifeScope(SubscribeViewModel(homeServiceQuickLiveData)) {
            newGetQuickServer()
        }
    }

    val homeServiceHistoryLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<GameService>>>()
    fun homeServiceHistory(isLoadMore: Boolean = false) {
        setRxLifeScope(object :
            SubscribeViewModel<HttpResponseDataE<GameService>>(homeServiceHistoryLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GameService>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            newGetHistoryService(page.toString(), limit.toString())
        }
    }

}